//true 为选择个人登录 false为选择企业登录
var is_user = true;

$(function(){
    var main = $('body');

    $('.login-form label').eq(0).css('borderBottom','0.1rem solid #4d5e73');

    $('.login-form label').eq(0).click(function () {
        $(this).css({'borderBottom':'0.1rem solid #4d5e73'});
        $(this).siblings('label').css('borderBottom','0.1rem solid transparent');
        is_user = true;
    });

    $('.login-form label').eq(1).click(function () {
        $(this).css({'borderBottom':'0.1rem solid #4d5e73'});
        $(this).siblings('label').css('borderBottom','0.1rem solid transparent');
        is_user = false;
    });

    // 点击登陆
    main.on('click','.btn-login',function () {
        if($("#phone").val() == ''){
            layer.msg('请输入职信账号');
        }else if($("#password").val() == ''){
            layer.msg('请输入密码');
        }else {

            // 个人账号登陆
            if(is_user == true){
                methods.post_user_login($("#phone").val(), $("#password").val())
            }

            // 企业账号登陆
            else{
                methods.post_company_login($("#phone").val(), $("#password").val())
            }
        }
    });
});

var methods = {
    // 个人登陆
    post_user_login: function (phone,password) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/user_login/',
            timeout: 6000,
            data: {
                password: password,
                phone: phone,
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200){
                    if(json.msg == '登陆成功，该用户为普通平台用户...'){
                        window.location.href='./user.html';
                        sessionStorage.setItem("user_id",json.data.id);
                        sessionStorage.setItem("user_name",json.data.name);
                        sessionStorage.setItem("user_phone",json.data.phone);
                        sessionStorage.setItem("credit_score",json.data.credit_score);
                        sessionStorage.setItem("resume_image",json.data.resume_image);
                        sessionStorage.setItem("image",json.data.image);
                        sessionStorage.setItem("birth",json.data.birth);
                        sessionStorage.setItem("gender",json.data.gender);
                    }else if(json.msg == '账号密码错误，请重试...'){
                        $("#phone").val('');
                        $("#password").val('');
                        layer.open({
                            title: '错误',
                            content: json.msg
                        });
                    }
                    else{
                        window.location.href='./user.html';
                        sessionStorage.setItem("user_id",json.data.id);
                        sessionStorage.setItem("user_name",json.data.name);
                        sessionStorage.setItem("user_phone",json.data.phone);
                        sessionStorage.setItem("company_name",json.data.company_name);
                        sessionStorage.setItem("company_id",json.data.company_id);
                        sessionStorage.setItem("department_name",json.data.department_name);
                        sessionStorage.setItem("duty",json.data.duty);
                        sessionStorage.setItem("credit_score",json.data.credit_score);
                        sessionStorage.setItem("image",json.data.image);
                        sessionStorage.setItem("resume_image",json.data.resume_image);
                        sessionStorage.setItem("birth",json.data.birth);
                        sessionStorage.setItem("gender",json.data.gender);
                    }
                }else {
                    layer.open({
                        title: '登陆失败',
                        content: json.msg
                    });
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 企业管理员登陆
    post_company_login: function (phone,password) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/company_login/',
            timeout: 6000,
            data: {
                password: password,
                phone: phone
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200){
                    if(json.msg == '账号密码错误，请重试...'){
                        $("#phone").val('');
                        $("#password").val('');
                        layer.open({
                            title: '错误',
                            content: json.msg
                        });
                    }else{
                        window.location.href='./company.html';
                        sessionStorage.setItem("user_id",json.data.id);
                        sessionStorage.setItem("user_name",json.data.name);
                        sessionStorage.setItem("user_phone",json.data.phone);
                        sessionStorage.setItem("company_name",json.data.company_name);
                        sessionStorage.setItem("company_id",json.data.company_id);
                        sessionStorage.setItem("company_info",json.data.company_info);
                        sessionStorage.setItem("company_image",json.data.company_image);
                        sessionStorage.setItem("auth",json.data.auth);
                        sessionStorage.setItem("total_employees",json.data.total_employees);
                        sessionStorage.setItem("total_departments",json.data.total_departments);
                    }
                }else {
                    layer.open({
                        title: '登陆失败',
                        content: json.msg
                    });
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    }
};